﻿using Abp.Application.Features;
using Abp.Application.Navigation;
using Abp.Authorization;
using Abp.Configuration;
using Abp.Configuration.Startup;
using Abp.Dependency;
using Abp.Localization;
using Abp.MultiTenancy;
using Abp.Runtime.Session;
using Abp.Web.Configuration;
using Abp.Web.Models.AbpUserConfiguration;
using Abp.Web.Security.AntiForgery;
using System.Threading.Tasks;

namespace Mt.Site.Core.Navigations.Navigation
{
    public class MUserConfigurationBuilder : AbpUserConfigurationBuilder
    {
        private readonly IIocResolver _iocResolver;
        private readonly IAbpStartupConfiguration _startupConfiguration;

        private readonly IMtUserNavigationManager _userNavigationManager;

        private readonly ITenantStore _tenantStore;

        public MUserConfigurationBuilder(IMultiTenancyConfig multiTenancyConfig,
            ILanguageManager languageManager,
            ILocalizationManager localizationManager,
            IFeatureManager featureManager,
            IFeatureChecker featureChecker,
            IPermissionManager permissionManager,
            IUserNavigationManager userNavigationManager,
            ISettingDefinitionManager settingDefinitionManager,
            ISettingManager settingManager,
            IAbpAntiForgeryConfiguration abpAntiForgeryConfiguration,
            IAbpSession abpSession,
            IPermissionChecker permissionChecker,
            IIocResolver iocResolver,
            IAbpStartupConfiguration startupConfiguration,
            IMtUserNavigationManager userNavigationManager2,
            ITenantStore tenantStore) 
            : base(multiTenancyConfig,
                languageManager,
                localizationManager,
                featureManager,
                featureChecker,
                permissionManager,
                userNavigationManager,
                settingDefinitionManager,
                settingManager,
                abpAntiForgeryConfiguration,
                abpSession,
                permissionChecker,
                iocResolver,
                startupConfiguration)
        {
            _iocResolver = iocResolver;
            _startupConfiguration = startupConfiguration;
            _userNavigationManager = userNavigationManager2;
            _tenantStore = tenantStore;
        }

        public override async Task<AbpUserConfigurationDto> GetAll()
        {
            return new MtUserConfigurationDto
            {
                MultiTenancy = GetUserMultiTenancyConfig(),
                Session = GetUserSessionConfig(),
                Localization = GetUserLocalizationConfig(),
                Features = await GetUserFeaturesConfig(),
                Auth = await GetUserAuthConfig(),
                Nav = null,
                Setting = await GetUserSettingConfig(),
                Clock = GetUserClockConfig(),
                Timing = await GetUserTimingConfig(),
                Security = GetUserSecurityConfig(),
                Custom = _startupConfiguration.GetCustomConfig(),
                Nav2 = await GetMenus() ,
                Session2 = GetUserSessionConfig2()
            };
        }

        protected MtUserSessionConfigDto GetUserSessionConfig2()
        {
            return new MtUserSessionConfigDto
            {
                UserId = AbpSession.UserId,
                TenantId = AbpSession.TenantId,
                ImpersonatorUserId = AbpSession.ImpersonatorUserId,
                ImpersonatorTenantId = AbpSession.ImpersonatorTenantId,
                MultiTenancySide = AbpSession.MultiTenancySide,
                CultureName = AbpSession.CultureName(),
                TenantName = AbpSession.TenantId == null ? null : _tenantStore.Find(AbpSession.TenantId.Value)?.TenancyName
            };
        }

        private async Task<MtUserNavConfigDto> GetMenus()
        {
            var userMenus = await _userNavigationManager.GetBackEndMenusAsync(AbpSession.ToUserIdentifier());

            var dto = new MtUserNavConfigDto
            { 
                Menus = userMenus
            };

            return dto;
        }
    }
}
